package com.example.demo.sensitive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.sensitive.dto.request.CheckTaskRequest;
import com.example.demo.sensitive.dto.response.CheckTaskResponse;
import com.example.demo.sensitive.service.CheckTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName CheckTaskController
 * @Description 敏感词检查任务控制器
 * @Author System
 * @Date 2025/9/19
 **/
@RestController
@RequestMapping("/api/sensitive-check")
@Tag(name = "敏感词检查", description = "敏感词检查任务相关操作接口")
public class CheckTaskController {

    @Resource
    private CheckTaskService checkTaskService;

    @GetMapping("/tasks")
    @Operation(summary = "分页查询检查任务", description = "根据条件分页查询检查任务列表")
    public Result<Page<CheckTaskResponse>> getPageList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int size,
            @Parameter(description = "任务状态") @RequestParam(required = false) String status,
            @Parameter(description = "开始时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        
        Page<CheckTaskResponse> result = checkTaskService.getPageList(current, size, status, startTime, endTime, keyword);
        return Result.success(result);
    }

    @GetMapping("/tasks/{id}")
    @Operation(summary = "查询任务详情", description = "根据ID查询检查任务详情")
    public Result<CheckTaskResponse> getById(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        CheckTaskResponse result = checkTaskService.getById(id);
        return Result.success(result);
    }

    @PostMapping("/start")
    @Operation(summary = "创建并启动检查任务", description = "创建新的敏感词检查任务并立即启动")
    public Result<String> startCheck(@Valid @RequestBody CheckTaskRequest request) {
        Long taskId = checkTaskService.createTask(request);
        checkTaskService.startTask(taskId);
        // 返回字符串格式的taskId以避免JavaScript精度问题
        return Result.success("任务创建并启动成功", String.valueOf(taskId));
    }

    @PostMapping("/tasks")
    @Operation(summary = "创建检查任务", description = "创建新的敏感词检查任务，不自动启动")
    public Result<String> createTask(@Valid @RequestBody CheckTaskRequest request) {
        Long taskId = checkTaskService.createTask(request);
        // 返回字符串格式的taskId以避免JavaScript精度问题
        return Result.success("任务创建成功", String.valueOf(taskId));
    }

    @PostMapping("/tasks/{id}/start")
    @Operation(summary = "启动任务", description = "启动指定的检查任务")
    public Result<String> startTask(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        checkTaskService.startTask(id);
        return Result.success("任务启动成功", "success");
    }

    @PostMapping("/stop/{id}")
    @Operation(summary = "停止任务", description = "停止正在执行的检查任务")
    public Result<String> stopTask(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        checkTaskService.stopTask(id);
        return Result.success("任务停止成功", "success");
    }

    @PostMapping("/tasks/{id}/cancel")
    @Operation(summary = "取消任务", description = "取消指定的检查任务")
    public Result<String> cancelTask(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        checkTaskService.cancelTask(id);
        return Result.success("任务取消成功", "success");
    }

    @DeleteMapping("/tasks/{id}")
    @Operation(summary = "删除任务", description = "删除指定的检查任务")
    public Result<String> deleteTask(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        checkTaskService.deleteTask(id);
        return Result.success("任务删除成功", "success");
    }

    @GetMapping("/progress/{id}")
    @Operation(summary = "获取任务进度", description = "获取指定任务的执行进度")
    public Result<CheckTaskResponse.TaskProgress> getTaskProgress(
            @Parameter(description = "任务ID") @PathVariable Long id) {
        
        CheckTaskResponse.TaskProgress result = checkTaskService.getTaskProgress(id);
        return Result.success(result);
    }

    @GetMapping("/running")
    @Operation(summary = "获取运行中任务", description = "获取所有正在运行的检查任务")
    public Result<List<CheckTaskResponse>> getRunningTasks() {
        List<CheckTaskResponse> result = checkTaskService.getRunningTasks();
        return Result.success(result);
    }

    @GetMapping("/pending")
    @Operation(summary = "获取待执行任务", description = "获取所有待执行的检查任务")
    public Result<List<CheckTaskResponse>> getPendingTasks() {
        List<CheckTaskResponse> result = checkTaskService.getPendingTasks();
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "任务统计", description = "获取任务执行统计信息")
    public Result<CheckTaskService.TaskStatistics> getTaskStatistics(
            @Parameter(description = "统计天数") @RequestParam(defaultValue = "30") int days) {
        
        CheckTaskService.TaskStatistics result = checkTaskService.getTaskStatistics(days);
        return Result.success(result);
    }

    @PostMapping("/handle-timeout")
    @Operation(summary = "处理超时任务", description = "处理超时的检查任务")
    public Result<String> handleTimeoutTasks(
            @Parameter(description = "超时分钟数") @RequestParam(defaultValue = "30") int timeoutMinutes) {
        
        checkTaskService.handleTimeoutTasks(timeoutMinutes);
        return Result.success("超时任务处理完成", "success");
    }
}
